﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Wheeler.Web.ViewModels.UtilityModels
{
    public class ListConfig
    {
        public ListConfig() { }
        public ListConfig(System.Data.DataTable dataSource, PagerConfig pager)
        {
            this.Pager = pager;
            this.DataSource = dataSource;
        }
        public String ControlID { get; set; }
        public bool ShowEditLink { get; set; }
        public String EditLinkFormat { get; set; }
        public bool ShowExternalLink { get; set; }
        public String ExternalLinkFormat { get; set; }
        public List<Group> Groups { get; set; }
        public System.Data.DataTable DataSource { get; set; }
        public PagerConfig Pager { get; set; }
        public bool ShowFieldName { get; set; }
        public bool MakeFieldNameBold { get; set; }

        #region Static Methods, List Config Builder

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputString">
        /// Separators
        /// (,) Comma denotes as group separator
        /// (|) Slash denotes as Field Separator
        /// (:) Denotes as Format Separator
        /// (~) Carret Denotes as Format Argument Separator
        /// <example>Group Name{ ID | Name | Password }, Personal Info{Email | Home | Address}, Picture{Image:<img src='/MyImages/{0}' alt='{1}' title='{2}' />(ThumbImage ~ ID ~ Name) }, Description{Description}</example>
        /// </param>
        /// <param name="dataSource"></param>
        /// <param name="showEditLink"></param>
        /// <param name="editLinkFormat"></param>
        /// <param name="showExternalLink">This variable only using for show contact list</param>
        /// <param name="externalLinkFormat">This variable only using for show contact list</param>
        /// <returns></returns>
        public static ListConfig Build(String inputString, System.Data.DataTable dataSource,
            bool showEditLink, String editLinkFormat, bool showExternalLink = false, String externalLinkFormat = "")
        {
            ListConfig config = new ListConfig();
            config.Groups = new List<Group>();
            String[] strGroups = inputString.Split(',');
            foreach (String strGroup in strGroups)
            {
                //String strGroupParts = strGroup.Substring(strGroup.IndexOf('{'), strGroup.Length);// strGroup.Split('{');
                //String[] groupParts = 
                Group group = new Group();
                group.Name = strGroup.Substring(0, strGroup.IndexOf('{')); //groupParts[0].Trim();

                ///Build the field Collection
                group.Fields = new List<Field>();
                int start = strGroup.IndexOf('{') + 1; int length = strGroup.LastIndexOf('}') - start;
                String strFields = strGroup.Substring(start, length);// strGroup.Split('{'); //groupParts[1].Trim();
                //strFields = strFields.Substring(0, strFields.LastIndexOf('}'));
                foreach (String strField in strFields.Split('|'))
                {
                    Field field = new Field();
                    if (strField.Contains(':'))
                    {
                        String[] fieldParts = strField.Split(':');
                        field.Name = fieldParts[0].Trim();

                        String[] formatParts = fieldParts[1].Split('(');
                        field.StringFormat = formatParts[0].Trim();

                        String[] strFormatFields = formatParts[1].Substring(0, formatParts[1].LastIndexOf(')')).Split('~');
                        field.Params = new List<string>();
                        foreach (String strFormatField in strFormatFields)
                        {
                            field.Params.Add(strFormatField.Trim());
                        }
                    }
                    else
                    {
                        field.Name = strField.Trim();
                    }
                    group.Fields.Add(field);
                }
                config.Groups.Add(group);
            }
            bindConfigProperties(config, dataSource, showEditLink, editLinkFormat, showExternalLink, externalLinkFormat);
            return config;
        }

        private static void bindConfigProperties(ListConfig config, System.Data.DataTable dt,
            bool showEditLink, string editLinkFormat, bool showExternalLink, String externalLinkFormat)
        {
            config.ShowEditLink = showEditLink;
            config.EditLinkFormat = editLinkFormat;
            config.ShowExternalLink = showExternalLink;
            config.ExternalLinkFormat = externalLinkFormat;
            config.ControlID = System.Guid.NewGuid().ToString();
            config.DataSource = dt;
        }
        #endregion

    }


    public class Group
    {
        public String Name { get; set; }
        public List<Field> Fields { get; set; }
    }

    public class Field
    {
        public String Name { get; set; }
        public String StringFormat { get; set; }
        public List<String> Params { get; set; }
    }
}